package com.itheima.reggie.controller;

import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.pojo.AddressBook;
import com.itheima.reggie.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * 地址管理
 */
@RestController
@RequestMapping("addressBook")
@Slf4j
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    /**
     * 查询登录用户的所有地址
     *
     * @return
     */
    @GetMapping("list")
    public R<List<AddressBook>> queryUserAddressBookList() {
        // 获取当前用户id
        Long userId = BaseContext.getCurrentId();
        List<AddressBook> addressBooks = addressBookService.lambdaQuery()
                .eq(AddressBook::getUserId, userId)
                .orderByDesc(AddressBook::getIsDefault)
                .orderByDesc(AddressBook::getUpdateTime)
                .list();
        return R.success(addressBooks);
    }

    /**
     * 添加地址
     * @param addressBook
     * @return
     */
    @PostMapping
    public R<String> addAddressBook(@RequestBody AddressBook addressBook) {
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBookService.save(addressBook);
        return R.success("添加地址成功");
    }

    /**
     * 根据地址id查询用户地址
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public R<AddressBook> queryAddressBookById(@PathVariable Long id) {
        AddressBook addressBook = addressBookService.getById(id);
        return R.success(addressBook);
    }

    /**
     * 用户修改地址
     * @param addressBook
     * @return
     */
    @PutMapping
    public R<String> modifyAddressBook(@RequestBody AddressBook addressBook) {
        addressBookService.updateById(addressBook);
        return R.success("用户地址修改成功");
    }

    /**
     * 用户删除地址
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> deleteAddressBook(Long[] ids) {
        addressBookService.removeByIds(Arrays.asList(ids));
        return R.success("删除地址成功");
    }

    /**
     * 获取用户的默认地址
     */
    @GetMapping("default")
    public R<AddressBook> queryDefaultAddressBook() {
        // 获取当前用户id
        Long userId = BaseContext.getCurrentId();
        // 查询当前用户是否有设置默认地址
        AddressBook defaultAddress = addressBookService.lambdaQuery()
                .eq(AddressBook::getUserId, userId)
                .eq(AddressBook::getIsDefault, 1)
                .one();
        if (defaultAddress == null) {
            return R.error("用户未设置默认地址");
        }
        return R.success(defaultAddress);
    }

    /**
     * 设置默认地址
     * @param addressBook
     * @return
     */
    @PutMapping("default")
    public R<String> updateDefaultAddressBook(@RequestBody AddressBook addressBook) {
        // 获取当前用户id
        Long userId = BaseContext.getCurrentId();
        // 获取用户的默认地址
        AddressBook defaultAddress = this.queryDefaultAddressBook().getData();
        if (defaultAddress == null) {
            //没有设置过默认地址
            addressBookService.lambdaUpdate()
                    .set(AddressBook::getIsDefault, 1)
                    .set(AddressBook::getUpdateTime, LocalDateTime.now())
                    .set(AddressBook::getUpdateUser, userId)
                    .eq(AddressBook::getId, addressBook.getId())
                    .eq(AddressBook::getUserId, userId)
                    .update();
            return R.success("设置默认地址成功");
        }
        // 修改原来的默认地址为非默认地址
        addressBookService.lambdaUpdate()
                .set(AddressBook::getIsDefault, 0)
                .set(AddressBook::getUpdateTime, LocalDateTime.now())
                .set(AddressBook::getUpdateUser, userId)
                .eq(AddressBook::getId, defaultAddress.getId())
                .eq(AddressBook::getUserId, userId)
                .update();
        // 设置新的默认地址
        addressBookService.lambdaUpdate()
                .set(AddressBook::getIsDefault, 1)
                .set(AddressBook::getUpdateTime, LocalDateTime.now())
                .set(AddressBook::getUpdateUser, userId)
                .eq(AddressBook::getId, addressBook.getId())
                .eq(AddressBook::getUserId, userId)
                .update();
        return R.success("修改默认地址成功");
    }
}
